package com.njupt.simulation.utils;
import cn.hutool.core.util.NumberUtil;

public class Antenna_672 {

    /**
     * 天线方向图
     * @param antennaType 天线种类(1.1和1.4)
     * @return
     */
    private static void calculateAntennaGain(String antennaType){
        if("1.1".equals(antennaType)){
//            calculateAntennaGain_SatSingleFeed();
        }else if("1.2".equals(antennaType)){
//            calculateAntennaGain_ClassA();
        }else if("1.3".equals(antennaType)){
//            calculateAntennaGain_ClassA_2();
        }else if("1.4".equals(antennaType)){
//            calculateAntennaGain_ClassB();
        }
    }

    /**
     * 1.1 卫星固定业务中单馈源的圆形或椭圆形波束航天器
     * @param Angle_Deviation 偏离主波束方向角度
     * @param MaxGain_MainLobe 主瓣最大增益
     * @param Bandwidth_Half3dB 3dB波束宽度的一半
     * @param ParaxialSidelobeLevel 相对于系统设计所要求峰值增益的近轴旁瓣电平 LN=-20
     * @param RadiatingBeam_Z 辐射波束的（主轴/次轴）z
     * @param LF 0
     * @return
     */
    public static Double calculateAntennaGain_SatSingleFeed(Double Angle_Deviation,
                                                            Double MaxGain_MainLobe,
                                                            Double Bandwidth_Half3dB,
                                                            Double ParaxialSidelobeLevel,
                                                            Double RadiatingBeam_Z,
                                                            Double LF){
        /*******中间变量  *****/
        Double B = 6.32;
        Integer Alpha = 2;

//        Double X = MaxGain_MainLobe + ParaxialSidelobeLevel + 25 * math.log(B*Bandwidth_Half3dB);
        Double p0 = NumberUtil.add(MaxGain_MainLobe, ParaxialSidelobeLevel);
        Double p1 = NumberUtil.mul(B, Bandwidth_Half3dB);
        Double p2 = Math.log(p1);
        Double p3 = 25.0;
        Double p4 = NumberUtil.mul(p3, p2);
        Double X = NumberUtil.add(p0, p4);

//      Double Y=B* Bandwidth_Half3dB*math.pow(10,0.04*( MaxGain_MainLobe+ ParaxialSidelobeLevel-0));
        Double k0 = NumberUtil.sub(p0, LF);
        Double k1 = 0.04;
        Double k2 = NumberUtil.mul(k1, k0);
        Double k3 = 10.0;
        Double k4 = Math.pow(k3,k2);
        Double Y  = NumberUtil.mul(Bandwidth_Half3dB, k4);

        Double A = 0.0;
        /*******计算流程图中的 A *****/
        Double a1 = 2.58 ;
        Double a2 = Math.log(RadiatingBeam_Z);
        if(ParaxialSidelobeLevel == -20){
            Double a3 = Math.sqrt ( 1 - a2 );
            A = NumberUtil.mul(a1,a3);
        }else if(ParaxialSidelobeLevel == -25){
            Double t1 = 0.8 ;
            Double t2 = NumberUtil.mul(t1,a2);
            Double a3 = Math.sqrt( 1 - t2 ) ;
            A = NumberUtil.mul(a1,a3);
        }

        /*******流程图中一共分为了6种情况，对应会产生6中结果 *****/
        /*******1、第一种结果 *****/
        Double b1 = NumberUtil.mul(A,Bandwidth_Half3dB);
        if(Bandwidth_Half3dB.compareTo(Angle_Deviation) <= 0 && Angle_Deviation.compareTo(b1) <= 0){
            Double c1 = NumberUtil.div(Angle_Deviation, Bandwidth_Half3dB);
            Double c2 = NumberUtil.mul(c1,c1);
            Double c3 = 3.0;
            Double c4 = NumberUtil.mul(c3,c2);
            Double AntennaGain_SSF = MaxGain_MainLobe - c4;
            return AntennaGain_SSF;
        }

        /*******2、第二种结果 *****/
        Double b2 = NumberUtil.mul(B,Bandwidth_Half3dB);
        if(b2.compareTo(Angle_Deviation) < 0 && Angle_Deviation.compareTo(Y) <= 0){
            Double c0 = Math.log(Angle_Deviation);
            Double c1 = 25.0;
            Double c2 = NumberUtil.mul(c1,c0);
            Double AntennaGain_SSF = NumberUtil.sub(X,c2);
            return AntennaGain_SSF;
        }

        /*******3、第三种结果 *****/
        Double b3 = 0.5;
        Double b4 = NumberUtil.mul(b2,b3);
        if(b1.compareTo(Angle_Deviation) < 0 && Angle_Deviation.compareTo(b4) <= 0){
            Double c1 = 20.0;
            Double c2 = NumberUtil.mul(c1,a2);
            Double AntennaGain_SSF = NumberUtil.add(p0,c2);
            return AntennaGain_SSF;
        }

        /*******4、第四种结果 *****/
        if(b4.compareTo(Angle_Deviation) < 0 && Angle_Deviation.compareTo(b2) <= 0){
            Double AntennaGain_SSF = p0;
            return AntennaGain_SSF;
        }

        /*******5、第五种结果 *****/
        Double b5 = 90.0;
        Double b6 = 180.0;
        if(b5.compareTo(Angle_Deviation) < 0 && Angle_Deviation.compareTo(b6) <= 0){
            Double c1 = 5.0;
            Double c2 = NumberUtil.mul(c1,a2);
            Double c3 = 0.25;
            Double c4 = NumberUtil.mul(c3,MaxGain_MainLobe);
            Double c5 = 15.0;
            Double c6 = NumberUtil.add(c5,ParaxialSidelobeLevel);
            Double c7 = NumberUtil.add(c6,c4);
            Double AntennaGain_SSF = NumberUtil.add(c7,c2);
            return AntennaGain_SSF;
        }

        /*******6、第六种结果 *****/
        if(Y.compareTo(Angle_Deviation) < 0 && Angle_Deviation.compareTo(b5) <= 0){
            Double AntennaGain_SSF = 0.0;
            return AntennaGain_SSF;
        }
        if(Angle_Deviation>=0 && Angle_Deviation<Bandwidth_Half3dB){
            return MaxGain_MainLobe;
        }
        return null;
    }


    /**
     * 1.2A类天线  S<=3.5
     * @param Delta_phy 凸状覆盖等值线到覆盖区外一个点在垂直于等值线各边方向上的角度
     * @param Ge Ge
     * @param f 频率 GHZ
     * @param diameter_D 实际直径D
     * @param Focus_F 反射器焦距F
     * @param Scanningratio_S 扫描比δ
     * @param diameter_d 计划的偏置抛物面口面直径d
     * @param reflectorEdgeHeight_h 反射器边缘的偏置高度h
     * @return
     */
    public static Double calculateAntennaGain_ClassA(Double Delta_phy,
                                                     Double Ge,
                                                     Double f,
                                                     Double diameter_D,
                                                     Double Focus_F,
                                                     Double Scanningratio_S,
                                                     Double diameter_d,
                                                     Double reflectorEdgeHeight_h){

        /*******中间变量  *****/
        //math.pow{10,[0.000075*(Scanningratio_S-0.5)2]/[( Focus_F/diameter_Dp)2 +0.02]2}

//        Double Phy0 = 72*( Wavelength_Lamda/diameter_D);
        Double Wavelength_Lamda = (3*1e8) / (f*1e9);
        Double c0 = 72.0;
        Double c1 = NumberUtil.div(Wavelength_Lamda,diameter_D);
        Double Phy0 = NumberUtil.mul(c0,c1);

        Double b0 = 3.0;
        Double Gep = NumberUtil.add(Ge, b0);

        Double b1 = NumberUtil.add(diameter_d, reflectorEdgeHeight_h);
        Double b2 = 2.0;
        Double diameter_Dp = NumberUtil.mul(b2,b1);

        Double ratio = NumberUtil.div(Delta_phy,Phy0);

        Double a0 = 0.5;
        Double a1 = NumberUtil.sub(Scanningratio_S, a0);
        Double a2 = Math.pow(a1, 2);
        Double a3 = 0.000075;
        Double a4 = NumberUtil.mul(a3, a2);

        Double a5 = NumberUtil.div(Focus_F, diameter_D);
        Double a6 = Math.pow(a5, 2);
        Double a7 = 0.02;
        Double a8 = NumberUtil.add(a7, a6);
        Double a9 = Math.pow(a8, 2);

        Double a10 = NumberUtil.div(a4, a9);
        Double Q = Math.pow(10, a10);

        /*******1、第一种结果 *****/
        Double A = 0.0;
        Double d = 0.8904;
        Double d1 = NumberUtil.mul(d,Q);
        if(A.compareTo(ratio) <= 0 && ratio.compareTo(d1) <= 0){
            //AntennaGain_ClassA = Gep + 0.256 - 13.065 * { [ Delta_phy/（Q*Phy0）] + 0.5 }2
            Double e0 = 0.256;
            Double e1 = NumberUtil.add(Gep,e0);

            Double e2 = 13.065;
            Double e3 =NumberUtil.mul(Q,Phy0);
            Double e4 =NumberUtil.div(Delta_phy,e3);
            Double e5 = 0.5;
            Double e6 = NumberUtil.add(e4,e5);

            Double e7 = Math.pow(e6, 2);
            Double e8 =NumberUtil.mul(e2, e7);

            Double e9 = NumberUtil.sub(e1,e8);
            return e9;
        }

        /*******2、第二种结果 *****/
        Double d2 = 1.9244;
        Double d3 = NumberUtil.mul(d2,Q);
        if(d1.compareTo(ratio) < 0 && ratio.compareTo(d3) <= 0){
            Double e0 = 25.0;
            Double e1 = NumberUtil.sub(Gep,e0);
            return e1;
        }

        /*******3、第三种结果 *****/
        Double d4 = 18.0;
        Double d5 = NumberUtil.div(d4,Phy0);
        if(d3.compareTo(ratio) < 0 && ratio.compareTo(d5) <= 0){
            Double e0 = 25.0;
            Double e1 = NumberUtil.sub(Gep,e0);

            //AntennaGain_ClassA= Gep-25+20*math.log(1.9244*Q*Phy0/ Delta_phy)

            Double e2 = NumberUtil.mul(d3,Phy0);
            Double e3 = NumberUtil.div(e2,Delta_phy);
            Double e4 = 20.0;
            Double e5 = Math.log(e3);
            Double e6 = NumberUtil.mul(e4,e5);
            Double e7 = NumberUtil.add(e1,e6);
            return e7;
        }

        return null;
    }

    /**
     * 1.3A类天线S大于5
     * @param Delta_phy 凸状覆盖等值线到覆盖区外一个点在垂直于等值线各边方向上的角度
     * @param Ge Ge
     * @param f 频率  GHZ
     * @param diameter_D 实际直径D
     * @param Focus_F 反射器焦距F
     * @param Scanningratio_S 扫描比δ
     * @return
     */
    public static Double calculateAntennaGain_ClassA_2(Double Delta_phy,
                                                       Double Ge,
                                                       Double f,
                                                       Double diameter_D,
                                                       Double Focus_F,
                                                       Double Scanningratio_S
    ){

        /*******中间变量  *****/
//        a)Delta_B= 1.65*math.pow[(diameter_D/ Wavelength_Lamda),-0.55]
//        b)B0=2.05+0.5*( Focus_F/ diameter_D-1)+0.0025* diameter_D/ Wavelength_Lamda
//        c)B = B0-( Scanningratio_S-1.25)* Delta_B
//        d)Phy_b=36* Wavelength_Lamda/ diameter_D
//        e)C=math.pow[(1+22/B),0.5]-1
        Double Wavelength_Lamda = (3*1e8) / (f*1e9);

        Double c0 = NumberUtil.div(diameter_D,Wavelength_Lamda);
        Double c1 = -0.55;
        Double c2 = Math.pow(c0, c1);
        Double c3 = 1.65;
        Double Delta_B = NumberUtil.mul(c3,c2);

        Double c4 = NumberUtil.div(Focus_F,diameter_D);
        Double c5 = 1.0;
        Double c6 = NumberUtil.sub(c4,c5);
        Double c7 = 0.5;
        Double c8 = NumberUtil.mul(c7,c6);
        Double c9 = 2.05;
        Double c10 = NumberUtil.add(c9,c8);

        Double c11 = 0.0025;
        Double c12 = NumberUtil.mul(c11,c0);
        Double B0 = NumberUtil.add(c10,c12);

        Double c13 = 1.25;
        Double c14 = NumberUtil.sub(Scanningratio_S,c13);
        Double c15 = NumberUtil.mul(c14,Delta_B);
        Double B = NumberUtil.sub(B0,c15);

        Double c16 = 36.0;
        Double c17 = NumberUtil.div(Wavelength_Lamda,diameter_D);
        Double Phy_b = NumberUtil.mul(c16,c17);

        Double c18 = 22.0;
        Double c19 = NumberUtil.div(c18,B);
        Double c20 = 1.0;
        Double c21 = NumberUtil.add(c20,c19);
        Double c22 = 0.5;
        Double c23 = Math.pow(c21,c22);
        Double C = NumberUtil.sub(c23,c20);

        /*******1、第一种结果 *****/
        Double A = 0.0;
        Double d1 = NumberUtil.mul(C,Phy_b);
        if(A.compareTo(Delta_phy) <= 0 && Delta_phy.compareTo(d1) <= 0){
            //AntennaGain_ClassA_2= Ge - B*{math.pow[ ( 1 + Delta_phy / Phy_b),2]-1}
            Double e0 = 1.0;
            Double e1 = NumberUtil.div(Delta_phy,Phy_b);
            Double e2 = NumberUtil.add(e0,e1);
            Double e3 = Math.pow(e2, 2);
            Double e4 =NumberUtil.sub(e3, e0);
            Double e5 =NumberUtil.mul(B, e4);
            Double e6 =NumberUtil.sub(Ge, e5);
            return e6;
        }

        /*******2、第二种结果 *****/
        Double d2 = 4.5;
        Double d3 = NumberUtil.add(C,d2);
        Double d4 = NumberUtil.mul(d3,Phy_b);
        if(d1.compareTo(Delta_phy) < 0 && Delta_phy.compareTo(d4) <= 0){
            Double e0 = 22.0;
            Double e1 = NumberUtil.sub(Ge,e0);
            return e1;
        }


        /*******3、第三种结果 *****/
        Double d5 = 18.0;
        if(d4.compareTo(Delta_phy) < 0 && Delta_phy.compareTo(d5) <= 0){
            //AntennaGain_ClassA_2= Gep-22+20*math.log((C+4.5)* Phy_b / Delta_phy)
            Double e0 = 22.0;
            Double e1 = NumberUtil.sub(Ge,e0);

            //AntennaGain_ClassA= Gep-25+20*math.log(1.9244*Q*Phy0/ Delta_phy)
            Double e8 = 4.5;
            Double e2 = NumberUtil.add(C,e8);
            Double e3 = NumberUtil.mul(e2,Phy_b);
            Double e4 = NumberUtil.div(e3,Delta_phy);
            Double e5 = Math.log(e4);
            Double e9 = 20.0;
            Double e6 = NumberUtil.mul(e9,e5);
            Double e7 = NumberUtil.add(e1,e6);
            return e7;
        }

        return null;
    }


    /**
         * 1.4 B类天线  S>=0
     * @param Delta_phy 凸状覆盖等值线到覆盖区外一个点在垂直于等值线各边方向上的角度
     * @param Ge Ge
     * @param f 频率
     * @param diameter_D 实际直径D
     * @param Focus_F 反射器焦距F
     * @param Scanningratio_S 扫描比δ
     * @return
     */
    public static Double calculateAntennaGain_ClassB(Double Delta_phy,
                                                     Double Ge,
                                                     Double f,
                                                     Double diameter_D,
                                                     Double Focus_F,
                                                     Double Scanningratio_S
    ){
        /*************中间变量：******/
//        a)Delta_B= 1.65*math.pow[(diameter_D/ Wavelength_Lamda),-0.55]
//        b)B0=2.05+0.5*( Focus_F/ diameter_D-1)+0.0025* diameter_D/ Wavelength_Lamda
//        c)B = B0-( Scanningratio_S-1.25)* Delta_B
//        d)Phy_b=36* Wavelength_Lamda/ diameter_D
//        e)C=math.pow[(1+17/B),0.5]-1

        Double Wavelength_Lamda = (3*1e8) / (f*1e9);
        Double c0 = NumberUtil.div(diameter_D,Wavelength_Lamda);
        Double c1 = -0.55;
        Double c2 = Math.pow(c0, c1);
        Double c3 = 1.65;
        Double Delta_B = NumberUtil.mul(c3,c2);

        Double c4 = NumberUtil.div(Focus_F,diameter_D);
        Double c5 = 1.0;
        Double c6 = NumberUtil.sub(c4,c5);
        Double c7 = 0.5;
        Double c8 = NumberUtil.mul(c7,c6);
        Double c9 = 2.05;
        Double c10 = NumberUtil.add(c9,c8);

        Double c11 = 0.0025;
        Double c12 = NumberUtil.mul(c11,c0);
        Double B0 = NumberUtil.add(c10,c12);

        Double c13 = 1.25;
        Double c14 = NumberUtil.sub(Scanningratio_S,c13);
        Double c15 = NumberUtil.mul(c14,Delta_B);
        Double B = NumberUtil.sub(B0,c15);

        Double c16 = 36.0;
        Double c17 = NumberUtil.div(Wavelength_Lamda,diameter_D);
        Double Phy_b = NumberUtil.mul(c16,c17);

        Double c18 = 17.0;
        Double c19 = NumberUtil.div(c18,B);
        Double c20 = 1.0;
        Double c21 = NumberUtil.add(c20,c19);
        Double c22 = 0.5;
        Double c23 = Math.pow(c21,c22);
        Double C = NumberUtil.sub(c23,c20);

        /*******1、第一种结果 *****/
        Double A = 0.0;
        Double d1 = NumberUtil.mul(C,Phy_b);
        if(A.compareTo(Delta_phy) <= 0 && Delta_phy.compareTo(d1) <= 0){
            //AntennaGain_ClassA_2= Ge - B*{math.pow[ ( 1 + Delta_phy / Phy_b),2]-1}
            Double e0 = 1.0;
            Double e1 = NumberUtil.div(Delta_phy,Phy_b);
            Double e2 = NumberUtil.add(e0,e1);
            Double e3 = Math.pow(e2, 2);
            Double e4 =NumberUtil.sub(e3, e0);
            Double e5 =NumberUtil.mul(B, e4);
            Double e6 =NumberUtil.sub(Ge, e5);
            return e6;
        }

        /*******2、第二种结果 *****/
        Double d2 = 1.0;
        Double d3 = NumberUtil.add(C,d2);
        Double d4 = NumberUtil.mul(d3,Phy_b);
        if(d1.compareTo(Delta_phy) < 0 && Delta_phy.compareTo(d4) <= 0){
            //AntennaGain_ClassB=Ge-17+18.7012*math.log[math.cos((Delta_phy-C* Phy_b)/ Phy_b)]
            Double e1 = NumberUtil.sub(Delta_phy,d1);
            Double e2 = NumberUtil.div(e1,Phy_b);
            Double e3 = Math.cos(e2);
            Double e4 = Math.log(e3);
            Double e5 = 18.7012;
            Double e6 = NumberUtil.mul(e5,e4);
            Double e7 = 17.0;
            Double e8 = NumberUtil.sub(Ge,e7);
            Double e9 = NumberUtil.sub(e8,e6);
            return e9;
        }

        /*******3、第三种结果 *****/
        Double d5 = 4.5;
        Double d6 = NumberUtil.add(C,d5);
        Double d7 = NumberUtil.mul(d6,Phy_b);
        if(d4.compareTo(Delta_phy) < 0 && Delta_phy.compareTo(d7) <= 0){
            Double e0 = 22.0;
            Double e1 = NumberUtil.sub(Ge,e0);
            return e1;
        }


        /*******4、第四种结果 *****/
        Double d8 = 18.0;
        if(d7.compareTo(Delta_phy) < 0 && Delta_phy.compareTo(d8) <= 0){
            //AntennaGain_ClassB=   Gep-22+20*math.log((C+4.5)* Phy_b / Delta_phy)
            Double e0 = 22.0;
            Double e1 = NumberUtil.sub(Ge,e0);

            //AntennaGain_ClassA= Gep-25+20*math.log(1.9244*Q*Phy0/ Delta_phy)
            Double e8 = 4.5;
            Double e2 = NumberUtil.add(C,e8);
            Double e3 = NumberUtil.mul(e2,Phy_b);
            Double e4 = NumberUtil.div(e3,Delta_phy);
            Double e5 = Math.log(e4);
            Double e9 = 20.0;
            Double e6 = NumberUtil.mul(e9,e5);
            Double e7 = NumberUtil.add(e1,e6);
            return e7;
        }

        return null;
    }
    public static void main(String[] args) {

        double a= calculateAntennaGain_SatSingleFeed(5.88950123517117,39.0,3.0,-20.0,1.0,0.0);
        double b= calculateAntennaGain_ClassA(1.0410466027218581,39.0,14.0,1.0,0.6,2.0,0.8,0.2);
        double c= calculateAntennaGain_ClassA_2(0.3123386648623507,31.0,14.5,1.0,0.6,6.0);
        double d= calculateAntennaGain_ClassB(1.7672682796632984,39.0,14.3,1.0,0.6,0.5);
        // 打印结果
        System.out.println("天线增益：" + a);
        System.out.println("天线增益：" + b);
        System.out.println("天线增益：" + c);
        System.out.println("天线增益：" + d);
    }
}
